package com.bong.panpan.bongcharttools.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bong.panpan.bongcharttools.R;
import com.bong.panpan.bongcharttools.custom.ObjectSaveUtil;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by panpan on 2017/9/25.
 */

public class CollectMACListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<CollectMACItem> items;
    private View.OnClickListener clickListener;

    public CollectMACListAdapter(Context context) {
        this.context = context;
        try {
            items= (ArrayList<CollectMACListAdapter.CollectMACItem>) ObjectSaveUtil.readCache("CollectMACListAdapter.dat");
        } catch (Exception e) {
            items=new ArrayList<>();
            e.printStackTrace();
        }
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public CollectMACItem getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_collect_mac, parent, false);
            holder.mac = (TextView) convertView.findViewById(R.id.text_mac);
            holder.headLayout = convertView.findViewById(R.id.layout_head);
            holder.head = (TextView) convertView.findViewById(R.id.text_head);
            holder.rssi = (TextView) convertView.findViewById(R.id.text_rssi);
            holder.button = (ImageButton) convertView.findViewById(R.id.button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CollectMACItem item = items.get(position);
        if (item.type == 0) {
            holder.button.setImageResource(R.mipmap.delete);
        } else {
            holder.button.setImageResource(R.mipmap.add);
        }
        if (position == 0) {
            holder.headLayout.setVisibility(View.VISIBLE);
            if (item.type == 0)
                holder.head.setText("设置采集的设备");
            else
                holder.head.setText("扫描到的设备");
        } else {
            CollectMACItem item2 = items.get(position - 1);
            if (item2.type != item.type) {
                holder.headLayout.setVisibility(View.VISIBLE);
                if (item.type == 0)
                    holder.head.setText("设置采集的设备");
                else
                    holder.head.setText("扫描到的设备");
            }
            else
            {
                holder.headLayout.setVisibility(View.GONE);
            }
        }
        if(item.type==1)
        {
            holder.rssi.setText(item.rssi+"dBm");
            holder.rssi.setVisibility(View.VISIBLE);
        }
        else
        {
            holder.rssi.setVisibility(View.GONE);
        }
        holder.button.setId(position*10+item.type);
        if (clickListener != null)
            holder.button.setOnClickListener(clickListener);
        holder.mac.setText("MAC:" + item.mac);
        return convertView;
    }

    public void setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public class ViewHolder {
        View headLayout;
        TextView head, mac,rssi;
        ImageButton button;
    }

    private boolean isItemExits(String mac) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).mac.equals(mac))
                return true;
        }
        return false;
    }

    public synchronized void modify(CollectMACItem item) {
        if (!isItemExits(item.mac)) {

        }
    }

    public int getItemPosition(String mac) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).mac.equals(mac))
                return i;
        }
        return -1;
    }

    public CollectMACItem getItem(String mac) {
        int position = getItemPosition(mac);
        return items.get(position);
    }

    private int getType0Count() {
        int count = 0;
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).type == 0)
                count++;
        }
        return count;
    }
    public ArrayList<CollectMACItem> getConfigItem()
    {
        ArrayList<CollectMACItem> result=new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).type == 0)
                result.add(items.get(i));
            else
                break;
        }
        return result;
    }
    public synchronized void remove(int index)
    {
        items.remove(index);
        notifyDataSetChanged();
    }
    public synchronized boolean add(CollectMACItem item) {
        if (item.type == 0) {
            int count = getType0Count();
            for (int i = 0; i < count; i++) {
                if (items.get(i).mac.equals(item.mac))//添加重复
                    return false;
            }
            items.add(count, item.clone());
        }else
        {
            int count = getType0Count();
            for (int i = count; i < items.size(); i++) {
                if (items.get(i).mac.equals(item.mac))//添加重复
                {
                    items.get(i).rssi=item.rssi;
                    notifyDataSetChanged();
                    return false;
                }
            }
            items.add(item.clone());
        }
        notifyDataSetChanged();
        return true;
    }

    public synchronized void clear() {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).type==1)
            {
                items.remove(i);
                i--;
            }
        }
        notifyDataSetChanged();
    }

    public static class CollectMACItem implements Serializable {
        public String mac = "";
        public int type = 1;
        public int rssi = -100;

        @Override
        public CollectMACItem clone() {
            CollectMACItem item=new CollectMACItem();
            item.mac=mac;
            item.rssi=rssi;
            item.type=type;
            return item;
        }
    }
}
